package com.fare.taluo.util;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



/**
 * 正则表达式方法用于解析和获取HTML内容
 * @author spring.华 
 * 	e-mail 13719279477@139.com
 * 2012-11-16
 */
public class RegHtmlUtil {
	
	public static final Log LOG = LogFactory.getLog(RegHtmlUtil.class);
	/**
	 * 用正则获取网上的信息
	 * @param str 
	 * @param mode "<span>\\s*\\n*<hr>\\s*\\n*\\t*<br>([^/]+)<br>\\s*\\n*\\t*</div>"
	 * @return
	 */
	public static String regStr(String sHtml,String mode){
		 Pattern p = Pattern.compile(mode,Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
		    Matcher m = p.matcher(sHtml);
		    String reg_s=null;
		    if (m.find()) {
		    	reg_s = m.group(1);
		    }
		   return  reg_s;
	}
	/**
	   * @param sHtml
	   * @param mode 正则
	   * @param  i  匹配第几个组
	   * @return
	   */
	  public static ArrayList<String> retrieveLinks(String sHtml, String mode,int i){
	    Pattern p = Pattern.compile(mode,Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
	    try{
	      if ((sHtml == null) || (sHtml == "")) return null;
	      Matcher m = p.matcher(sHtml);
	      ArrayList <String> linkList = new ArrayList <String> ();
	      while (m.find()) {
	        String link = m.group(i).trim();
	        linkList.add(link);
	      }
	      return linkList;
	    } catch (RuntimeException e) {
	    	LOG.info("正则匹配出现异常!");
	    	e.printStackTrace();
	    }
	    return null;
	  }
	  /**
	   * @param sHtml
	   * @param mode 正则
	   * @param  i  匹配第几个组
	   * @return
	   */
	  public static ArrayList<String> retrieveLinks(String sHtml, String mode,int i,Integer pattern){
	    Pattern p = Pattern.compile(mode,pattern);
	    try{
	      if ((sHtml == null) || (sHtml == "")) return null;
	      Matcher m = p.matcher(sHtml);
	      ArrayList <String> linkList = new ArrayList <String> ();
	      while (m.find()) {
	        String link = m.group(i).trim();
	        linkList.add(link);
	      }
	      return linkList;
	    } catch (RuntimeException e) {
	    	LOG.info("正则匹配出现异常!");
	    	e.printStackTrace();
	    }
	    return null;
	  }
	  /**
		 * 用正则获取网上的信息
		 * @param str 
		 * @param mode "<span>\\s*\\n*<hr>\\s*\\n*\\t*<br>([^/]+)<br>\\s*\\n*\\t*</div>"
		 * @param  i  匹配第几个组
		 * @return
		 */
		public static String regStr(String sHtml,String mode,int i){
			 Pattern p = Pattern.compile(mode,Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
			    Matcher m = p.matcher(sHtml);
			    String reg_s=null;
			    if (m.find()) {
			    	reg_s = m.group(i);
			    }
			   return  reg_s;
		}
		 /**
		 * 用正则获取网上的信息
		 * @param str 
		 * @param mode "<span>\\s*\\n*<hr>\\s*\\n*\\t*<br>([^/]+)<br>\\s*\\n*\\t*</div>"
		 * @param  i  匹配第几个组
		 * @return
		 */
		public static String regStr(String sHtml,String mode,int i,int pattern){
			 Pattern p = Pattern.compile(mode,pattern);
			    Matcher m = p.matcher(sHtml);
			    String reg_s=null;
			    if (m.find()) {
			    	reg_s = m.group(i);
			    }
			   return  reg_s;
		}
		/**
		 * 验证字符串是否符合某个正则式
		 * @param str
		 * @param regex
		 * @return
		 */
		public static boolean regMathcher(String str, String regex){
			Pattern p = Pattern.compile(regex);
		    Matcher m = p.matcher(str);
		    return m.find();
		}
		/**
		 * 验证一组表达式里，是否有符合的字符串
		 * @param shtml
		 * @param modes
		 * @return
		 */
		public static String regStr(String shtml, List<String> modes){
			if(modes == null || modes.isEmpty()) return null;
			
			for(String s : modes){
				String result = regStr(shtml, s, 0, Pattern.CASE_INSENSITIVE+Pattern.DOTALL);
				if(StringUtils.isNotBlank(result)){
					return result;
				}
			}
			return null;
		}
		public static void  main(String[] arg) throws IOException{
			String html = "10.108.1.224:d8080";
			
			
		    //System.out.println(m.find());
		}
}
